package com.example.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.springcloud.alibaba.client.OrderClient;
import com.example.springcloud.alibaba.client.StorageClient;
import com.example.springcloud.alibaba.myHandler.CustomerBlockHandler;
import com.example.springcloud.entities.CommonResult;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
public class BusinessController {
    @Resource
    private OrderClient orderClient;
    @Resource
    private StorageClient storageClient;

    @GetMapping(value = "buy")
    @GlobalTransactional(rollbackFor = Exception.class)
//    @SentinelResource(value = "customerBlockHandler",
//            blockHandlerClass = CustomerBlockHandler.class,
//            fallbackClass = CustomerBlockHandler.class,
//            fallback = "handlerException3",
//            blockHandler = "handlerException2")
    public CommonResult create(Long userId,Long productId) throws Exception {
        if(productId == 1) {
            throw new Exception("模拟异常");
        }
        orderClient.create(userId,productId);
        storageClient.changeStorage(productId,1);
        return new CommonResult(200,"购买成功");
    }
}
